package com.seven.webhook.simple.model;

import java.math.BigDecimal;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;

import java.io.Serializable;

import com.seven.webhook.mapper.MonitorRuleMapper;
import com.seven.webhook.model.MonitorRule;
import com.seven.webhook.simple.model.enm.BaseIndexEnum;
import com.seven.webhook.simple.model.enm.RuleSymbolEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * <p>
 *
 * </p>
 *
 * @author seven
 * @since 2020-11-13
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class MonitorSimpleRule implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private String id;
    private String name;
    @TableField("rule_name")
    private String ruleName;
    private String ip;
    @TableField("base_index")
    private BaseIndexEnum baseIndex;
    @TableField("rule_symbol")
    private RuleSymbolEnum ruleSymbol;
    private BigDecimal value;
    @TableField("is_alert")
    private Boolean isAlert;
    @TableField("for_time")
    private String forTime;

    public String getRuleName() {
        if (ruleName==null)ruleName=ip + baseIndex.getCode();
        return ruleName;
    }

    public MonitorRule parse2MonitorRule() {
        MonitorRule monitorRule = new MonitorRule();
        BeanUtil.copyProperties(this, monitorRule);
        monitorRule.setExpr(baseIndex.getExpr(ip) + ruleSymbol.getDes() + value);
        monitorRule.setSeverity("pate");
        monitorRule.setDescription("$labels.instance " + baseIndex.getDes() + ruleSymbol.getDes() + value + "%" + " 当前值：$value%");
        monitorRule.setSummary("$labels.instance " + baseIndex.getDes() + "告警");
        // TODO: 14/11/2020 重新设计一下
        monitorRule.setMonitorGroupId("3");
        monitorRule.setInstance(ip + ":" + baseIndex.getPort());
        return monitorRule;
    }
}


